package com.qxsx.midi.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.qxsx.midi.R;
import com.qxsx.midi.activity.exam.ClassVideoDetailsActivity;
import com.qxsx.midi.config.AppContext;
import com.qxsx.midi.model.ClassVideo;
import com.qxsx.midi.utils.MyNet;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Zrj on 2015/9/7.
 */
public class       ClassVideoAdapter extends BaseAdapter {

    private List<ClassVideo> list = new ArrayList<>();

    private LayoutInflater inflater;
    private Context context;

    public ClassVideoAdapter(Context context, List<ClassVideo> list) {
        this.context = context;
        this.list = list;
        inflater = LayoutInflater.from(context);
    }

    //动态添加数据
    public void addData(List<ClassVideo> addlist) {//刷新数据
        for (ClassVideo classVideo : addlist) {
            list.add(classVideo);
        }
        notifyDataSetChanged();
    }
    // 清空所有
    public void removeAll()
    {
        list.clear();
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.classvideo_list_item, null);
            holder.iv_img = (ImageView) convertView.findViewById(R.id.iv_img);
            holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            holder.tv_content = (TextView) convertView.findViewById(R.id.tv_content);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final ClassVideo classVideo = list.get(position);
        MyNet.setNetImage(AppContext.getContext(), holder.iv_img, classVideo.getClv_img());
        holder.tv_name.setText(classVideo.getClv_name());
        holder.tv_content.setText(classVideo.getClv_content());
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it = new Intent(context, ClassVideoDetailsActivity.class);
                it.putExtra("id", classVideo.getClv_id());//传递商家ID
                context.startActivity(it);

            }
        });
        return convertView;
    }

    class ViewHolder {
        ImageView iv_img;
        TextView tv_name, tv_content;

    }

}
